﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using personalplaner.common.controls;
using personalplaner.plugins.plananalysis.viewmodels;
using personalplaner.business.dienstplan;
using System.Collections.ObjectModel;

namespace personalplaner.plugins.plananalysis
{
	/// <summary>
	/// Interaction logic for HolidayOverview.xaml
	/// </summary>
	public partial class HolidayOverview : BaseControl
	{
		public HolidayOverview()
		{
			InitializeComponent();
			var dc = DataContext as HolidayOverviewViewModel;
			if (dc != null)
				dc.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(dc_PropertyChanged);
		}

		void dc_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "Collection")
				Initialize();
		}

		public void Initialize()
		{
			var grid = DiensteGrid;
			if (grid == null)
				return;

			var da = DataContext as HolidayOverviewViewModel;
			if (da == null)
				return;

			var Collection = da.Collection;
			if (Collection == null)
				return;

			var planItems = new List<IList<PlanItem>>();
			foreach (var pi in Collection)
				planItems.Add(new ObservableCollection<PlanItem>(pi));

			grid.Columns.Clear();

			if (planItems.Count <= 0)
				return;

			int index = 0;
			foreach (var column in planItems[0])
			{
				var col = new DataGridPlanItemTemplateColumn
				{
					IsReadOnly = true,
					CanUserReorder = false,

					Header = column.Datum.Day,

					ColumnName = string.Format("[{0}]", index),
					CellTemplate = (DataTemplate)FindResource("PART_CellTemplate")
				};

				index++;
				grid.Columns.Add(col);
			}

			grid.ItemsSource = planItems;
		}

		public FrameworkElement Grids
		{
			get
			{
				return grids;
			}
		}
	}
}
